package com.gopay.common.user.manager.impl;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Service;

import com.gopay.common.domain.UserInfo;
import com.gopay.common.user.dao.UserInfoQueryDAO;
import com.gopay.common.user.manager.UserInfoQueryManager;
import com.gopay.common.util.NonrevEncr;
import com.gopay.common.util.pwd.UserPwdUtil;

/**
 * userInfoQuery Manager实现
 * 
 * @userInfoQueryManager.java
 * @author liuxin
 * @2013-1-6 下午1:52:09 www.gopay.com.cn Inc.All rights reserved.
 */
@Service("userInfoQueryManager")
public class UserInfoQueryManagerImpl implements UserInfoQueryManager {

    @Resource(name = "userInfoQueryDao")
    private UserInfoQueryDAO userInfoQueryDao;

    @Override
    public UserInfo get(Serializable id) {
        return userInfoQueryDao.find(UserInfo.class, id);
    }

    @Override
    public List<UserInfo> get(Map<Object, Object> paraMap) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public List<UserInfo> getAll() {
        return userInfoQueryDao.findAll(UserInfo.class);
    }

    @Override
    public UserInfo getUserInfoByEmail(String email) {
        return userInfoQueryDao.getUserInfoByEmail(email);
    }

    @Override
    public List<UserInfo> getByCustId(String custId) {
        return userInfoQueryDao.getByCustId(custId);
    }

    /* (non-Javadoc)
     * @see com.gopay.common.user.manager.UserInfoQueryManager#getUserInfoByEmailAndOprId(java.lang.String, java.lang.String, java.lang.String)
     */
    @Override
    public UserInfo getUserInfoByEmailAndOprId(String email, String oprId) {
        // TODO Auto-generated method stub
        return userInfoQueryDao.getUserInfoByEmailAndOprId(email, oprId);
    }

	@Override
	public UserInfo getUserInfoByCustIdAndOprId(String custId, String oprId) {
		return userInfoQueryDao.getUserInfoByCustIDAndOprId(custId, oprId);
	}
	@Override
	public UserInfo getUserInfoByUserId(String userId) {
		return userInfoQueryDao.getUserInfoByUserId(userId);
	}

    @Override
    public UserInfo getCorpManager(String custId) {
        DetachedCriteria criteria = DetachedCriteria.forClass(UserInfo.class);
        criteria.add(Restrictions.eq("custId",custId)).add(Restrictions.eq("operId", "999"));
        List<UserInfo> result = userInfoQueryDao.findByCriteria(criteria);
        if(result != null && result.size() > 0){
            return result.get(0);
        }
        return null;
    }

    @Override
	public UserInfo getBySSoId(String ssoId) {
		return userInfoQueryDao.getBySSoId(ssoId);
	}

	@Override
	public UserInfo getUserInfoByMobile(String mobile) {
		return userInfoQueryDao.getUserInfoByMobile(mobile);
	}

	@Override
	public UserInfo getUserInfoByCustId(String custId) {
		List<UserInfo> list = userInfoQueryDao.getByCustId(custId);
		return list==null?null:list.get(0);
	}

	@Override
    public void setUserInfoPassword(StringBuilder sbNewPwd, UserInfo userInfo) {
        if(UserPwdUtil.isSHA1(userInfo.getPassword())){
            userInfo.setPassword(NonrevEncr.sha512(sbNewPwd));
        }
    }
}
